package second.actor

import scala.actors.Actor

/**
  * Created by feng on 24/11/2018.
  */
class AppleActor extends Actor{

  override def act(): Unit = {

    while (true){
      receive{
        case "start" => println("start ...")
        case SyncMsg(id,msg) =>{
          println(id+"，"+msg)
          Thread.sleep(1000)
          sender ! ReplyMsg(3,"finished")
        }

        case AsyncMsg(id,msg) =>{
          println(id+","+msg)
          Thread.sleep(2000)
        }
      }
    }
  }
}


object AppleActor{
  def main(args: Array[String]): Unit = {
    val actors = new AppleActor
    actors.start()
//    actors ! AsyncMsg(1,"hello")

    val replyMsg = actors !! SyncMsg(2,"word")
    println("replyMsg.isSet=1="+replyMsg.isSet)
    val c = replyMsg.apply()
    println("c1="+c)
    println("replyMsg.isSet=2="+replyMsg.isSet)
    println("==")
    println(c)
  }
}

case class SyncMsg(id:Int,name:String)
case class AsyncMsg(id:Int,name:String)
case class ReplyMsg(id:Int,name:String)